package com.github.aaronshan.functions.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.junit.Test;

import java.util.Map;

/**
 * @Auther: GGbond
 * @Date: 2020/3/23 17:49
 * @Description:
 */
public class UDFGetObjectOneByKeys {

    @Test
    public void run() {
        System.out.println(evaluate("{\"SystemTime\":\"2020-03-23 17:18:55\",\"AdminDivisionCode\":\"440115\",\"Lat\":22.682771,\"Location\":\"广东广州南沙区\",\"PowerOff\":0,\"Direction\":0,\"GpsTime\":\"2020-03-23 09:18:49\",\"Lng\":113.579647,\"GpsAntennaShort\":0,\"LocationStatus\":1,\"Altitude\":0,\"LockStatus\":0,\"Speed\":0,\"GpsAntennaBreak\":0,\"EncryptLng\":113.58470854887271,\"VehicleId\":\"0572b0722aa041c886499b34b33c7535\",\"AccStatus\":0,\"LowVoltage\":0,\"GpsModuleFault\":0,\"EncryptLat\":22.679744647129848}", "speed", "lat"));
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            evaluate("{\"AdminDivisionCode\":\"440115\",\"EngineFuelRate\":11.15,\"PowerOff\":0,\"LocationStatus\":1,\"LockStatus\":0,\"Altitude\":1,\"GpsAntennaBreak\":0,\"TotalEngineHours\":721.40,\"AccStatus\":1,\"CurrentSpeedLoadPercentage\":42,\"AtmosphericPressure\":101.50,\"GpsSatellite\":21,\"TotalFuelConsum\":5151.50,\"Lat\":22.681413,\"IntakeManifoldTemperature\":36,\"PercentTorque\":30,\"EngineCoolantLevel\":102,\"GpsTime\":\"2020-03-25 00:49:50\",\"spn1\":0,\"spn2\":0,\"spn3\":0,\"spnmsb1\":0,\"spnmsb2\":0,\"spn4\":0,\"IntakeTemperature\":36,\"CurrentThrottleValue\":54.80,\"SystemTime\":\"2020-03-25 08:50:02\",\"GpsVoltage\":27.98,\"EngineOilTemperature\":1774.97,\"Direction\":157,\"WorkDataTime\":\"2020-03-25 00:49:50\",\"GpsAntennaShort\":0,\"VehicleId\":\"0572b0722aa041c886499b34b33c7535\",\"EncryptLng\":113.5858960087828,\"GpsModuleFault\":0,\"EncryptLat\":22.678385387214806,\"SPNconversionmode2\":0,\"fmi1\":0,\"EngineRotateSpeed\":1425,\"EngineOilPressure\":296,\"AccOnHours\":723.30,\"Location\":\"广东广州南沙区\",\"currentfaultcount2\":0,\"currentfaultcount1\":0,\"SPNconversionmode1\":0,\"Lng\":113.580836,\"Speed\":4,\"LowVoltage\":0,\"GsmSignal\":15,\"AmbientTemperature\":-40,\"EngineCoolantTemperature\":83}", "speed", "lat");
        }
        System.out.println(System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            evaluate1("{\"AdminDivisionCode\":\"440115\",\"EngineFuelRate\":11.15,\"PowerOff\":0,\"LocationStatus\":1,\"LockStatus\":0,\"Altitude\":1,\"GpsAntennaBreak\":0,\"TotalEngineHours\":721.40,\"AccStatus\":1,\"CurrentSpeedLoadPercentage\":42,\"AtmosphericPressure\":101.50,\"GpsSatellite\":21,\"TotalFuelConsum\":5151.50,\"Lat\":22.681413,\"IntakeManifoldTemperature\":36,\"PercentTorque\":30,\"EngineCoolantLevel\":102,\"GpsTime\":\"2020-03-25 00:49:50\",\"spn1\":0,\"spn2\":0,\"spn3\":0,\"spnmsb1\":0,\"spnmsb2\":0,\"spn4\":0,\"IntakeTemperature\":36,\"CurrentThrottleValue\":54.80,\"SystemTime\":\"2020-03-25 08:50:02\",\"GpsVoltage\":27.98,\"EngineOilTemperature\":1774.97,\"Direction\":157,\"WorkDataTime\":\"2020-03-25 00:49:50\",\"GpsAntennaShort\":0,\"VehicleId\":\"0572b0722aa041c886499b34b33c7535\",\"EncryptLng\":113.5858960087828,\"GpsModuleFault\":0,\"EncryptLat\":22.678385387214806,\"SPNconversionmode2\":0,\"fmi1\":0,\"EngineRotateSpeed\":1425,\"EngineOilPressure\":296,\"AccOnHours\":723.30,\"Location\":\"广东广州南沙区\",\"currentfaultcount2\":0,\"currentfaultcount1\":0,\"SPNconversionmode1\":0,\"Lng\":113.580836,\"Speed\":4,\"LowVoltage\":0,\"GsmSignal\":15,\"AmbientTemperature\":-40,\"EngineCoolantTemperature\":83}", "speed", "lat");
        }
        System.out.println(System.currentTimeMillis() - start);

    }

    public String evaluate(String json, String... params) {
        try {
            Map<String, Object> dataOrg = (Map<String, Object>) JSON.parse(json);
            Map<String, Object> data = new CaseInsensitiveMap();
            if (dataOrg != null) {
                data.putAll(dataOrg);
            }
            for (String param : params) {
                if (data.containsKey(param)) {
                    return data.get(param) == null ? null : data.get(param).toString();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public String evaluate1(String json, String... params) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            for (String param : params) {
                if (jsonObject.containsKey(param)) {
                    return jsonObject.get(param) == null ? null : jsonObject.get(param).toString();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
